<?php
declare (strict_types = 1);

namespace app\api\controller\v1;

use app\api\helpers\v1\traits\FileUpload;
use app\api\helpers\v1\traits\SearchDataForModel;
use app\api\model\v1\AdminLog;
use app\api\model\v1\Admins;
use app\api\model\v1\Community;
use app\api\model\v1\ProjectSample;
use app\BaseController;
use think\Model;
use think\Request;

class CommunityCon extends BaseController
{
    use SearchDataForModel,FileUpload;

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2021/12/29 14:19
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * 社区列表 -- 数据源
     */
    public function getCommunityList(){
        $limit = $this->request->param('limit',10);
        $type = $this->request->param('type','page');
        if ($type === 'no_page'){
            $res = Community::field('id as value,community_title as label')->select();
        }else{
            $res = $this->search(new Community(),[],$limit,'desc',['area','street','gatherCompany','admin']);
        }

        return $this->response($res);
    }


    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/1/7 14:16
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * 根据单位ID获取对应的人员信息
     */
    public function getAdminAndCompanyId(){
        $limit = $this->request->param('limit',10);
        $unit_id = $this->request->param('unit_id',0);
        $admin = Admins::where(['unit_id'=>$unit_id])->paginate($limit);
        return $this->response($admin);
    }


    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2021/12/29 14:15
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 导入县区，街道，社区，单位
     */
    public function importCommunityExcel(){
        if (!$this->request->file('file')) {
            return $this->response(501, '请选择上传文件');
        }
        $file = $this->request->file('file');
        $result = $this->saveImportExcel($file, 'import_excel',$type='community');
        $is_bool = Community::saveImportExcel($result,$this->request->uid);
        if ($is_bool === 200) {
            //日志
            AdminLog::record('导入县区，街道，社区，单位模版', $this->request->param(), $this->request->uid, $this->request->username);
            return $this->response();
        } else {
            return $this->response(403, '导入问卷失败');
        }
    }


    /**
     * Created by PhpStorm.
     * User: 刘海龙
     * Date: 2022/1/1
     * Time: 20:56
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 获取当前社区下的样本点
     */
    public function getCommunitySample(){
        $limit = $this->request->param('limit',10);
        $community_id = $this->request->param('community_id',0);
        $community = Community::find($community_id);
        if (is_null($community)){
            return $this->response(403,'暂未找到该社区');
        }
        $res = ProjectSample::where(['community_id'=>$community_id])->paginate($limit);
        return  $this->response($res);
    }
}
